<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');
session_start();

class profile extends CI_Controller {

    public function __construct() {
        parent::__construct();
        if ($this->session->userdata('logged_in_user')) {
            $this->logged_in = TRUE;
        } else {
            redirect('login/', 'refresh');
        }
        $this->load->model('Dao');
        $this->load->helper('date');
    }

    public function index($defaultTab = "photo") {
        $user = $this->session->userdata('logged_in_user');
        $user = $this->Dao->findUserById($user->id);
        $data['title'] = 'Welcome to wfaces';
        $data['description'] = 'NONE';
        $data['keywords'] = 'Key, words, of, the, page';
        $data['view'] = 'v_profile';
        $data['user'] = $user;
        $data['defaultTab'] = $defaultTab;
        $this->load->view('templates/template', $data);
    }

    public function of($id) {
        $user = $this->Dao->findUserById($id);
        $data['title'] = 'Welcome to wfaces';
        $data['description'] = 'NONE';
        $data['keywords'] = 'Key, words, of, the, page';
        $data['view'] = 'v_profile_of';
        $data['user'] = $user;
        $this->load->view('templates/template', $data);
    }

    public function saveTof() {
        $path = ASSETSPATH . "pht";
//        $path = "/var/www/wfaces/assets/pht";
//        echo $path;
//        return;
        $config['upload_path'] = $path;
        $config['allowed_types'] = 'gif|jpg|png';
        $this->load->library('upload', $config);
        $user_id = $this->session->userdata('logged_in_user')->id;
        if (!$this->upload->do_upload("tof")) {
//            echo ASSETSPATH;
//            br();
            $error = array('error' => $this->upload->display_errors());
            print_r($error);
            return;
        } else {
            $tof = $user_id . strtolower($this->upload->file_ext);
            if ($this->upload->rename_file($this->upload->file_name, $tof)) {
                $this->Dao->updateTof($user_id, $tof);
            }
        }
        $this->session->set_userdata('logged_in_user', $this->Dao->findUserById($user_id));
        redirect('profile/index');
    }

    public function saveProfile($flag = "1") {
        $birthday = $this->input->post("birthday");

        if ($birthday != null && $birthday != "") {
            $dt = explode("/", $birthday);
            $d = $dt[0];
            $m = $dt[1];
            $y = $dt[2];
            $birthday = $y . "-" . $m . "-" . $d;
        }
        $presentation = $this->input->post("presentation");
        $user_id = $this->session->userdata('logged_in_user')->id;
        $this->Dao->updateUserProfile($user_id, $birthday, $presentation);
        $this->session->set_userdata('logged_in_user', $this->Dao->findUserById($user_id));
        echo $flag;
    }

    public function saveContact($flag = "1") {
        $city = $this->input->post("city");
        $country = $this->input->post("country");
        $mobile = $this->input->post("mobile");
        $user_id = $this->session->userdata('logged_in_user')->id;

        $data = array();
        if ($city != null) {
            $city = $this->Dao->findCityByName($city);
            $data['city'] = $city;
        }

        if ($country != null) {
            $country = $this->Dao->findCountryByName($country);
            $data['country'] = $country;
        }
        if ($mobile != null) {
            $data['mobile'] = $mobile;
        }
        $this->Dao->updateContact($user_id, $data);

        $this->session->set_userdata('logged_in_user', $this->Dao->findUserById($user_id));
        echo $flag;
    }

    public function saveSocial($flag = "1") {
        $user_id = $this->session->userdata('logged_in_user')->id;
        $fb = $this->input->post("fb");
        $twitter = $this->input->post("twitter");
        $this->Dao->deleteNetworksFor($user_id);
        $this->Dao->addFbAndTwitter($user_id, $fb, $twitter);
        $this->session->set_userdata('logged_in_user', $this->Dao->findUserById($user_id));
        echo $flag;
    }

    public function editProfile() {
        $user = $this->session->userdata('logged_in_user');
        $user = $this->Dao->findUserById($user->id);
        $data['title'] = 'Welcome to wfaces';
        $data['description'] = 'NONE';
        $data['keywords'] = 'Key, words, of, the, page';
        $data['view'] = 'v_profile_modal';
        $data['user'] = $user;
        $this->load->view('v_profile_modal', $data);
    }

    public function editPhoto() {
        $user = $this->session->userdata('logged_in_user');
        $user = $this->Dao->findUserById($user->id);
        $data['title'] = 'Welcome to wfaces';
        $data['description'] = 'NONE';
        $data['keywords'] = 'Key, words, of, the, page';
        $data['view'] = 'v_photo_modal';
        $data['user'] = $user;
        $this->load->view('v_photo_modal', $data);
    }

    public function editContact() {
        $user = $this->session->userdata('logged_in_user');
        $user = $this->Dao->findUserById($user->id);
        $data['title'] = 'Welcome to wfaces';
        $data['description'] = 'NONE';
        $data['keywords'] = 'Key, words, of, the, page';
        $data['view'] = 'v_contact_modal';
        $data['user'] = $user;
        $this->load->view('v_contact_modal', $data);
    }

    public function editSocial() {
        $user = $this->session->userdata('logged_in_user');
        $user = $this->Dao->findUserById($user->id);
        $data['title'] = 'Welcome to wfaces';
        $data['description'] = 'NONE';
        $data['keywords'] = 'Key, words, of, the, page';
        $data['view'] = 'v_social_modal';
        $data['user'] = $user;
        $this->load->view('v_social_modal', $data);
    }

    public function saveEdited($tab) {
        if ($tab == "profile") {
            $this->saveEditedPorfile();
        } else if ($tab == "contact") {
            $this->saveContact("");   //DONT ECHO anything pleeeeeeeeeeeeeaaaaaaaaaaaaaaaaaaaaaaseee!!!!!!!!!!!
        } else if ($tab == "social") {
            $this->saveSocial("");
        }
        redirect('profile/index/' . $tab, 'refresh');
    }

    public function saveEditedPorfile() {
        $dataUser = array();
        $dataUser['birthday'] = $this->input->post("birthday");
        $dataUser['firstname'] = $this->input->post("firstname");
        $dataUser['lastname'] = $this->input->post("lastname");
        $dataUser['mail'] = $this->input->post("mail");
        $dataUser['presentation'] = $this->input->post("presentation");
        $dataUser['sexe'] = $this->input->post("sexe");
        //echo $dataUser['birthday'];

        if ($dataUser['birthday'] != null && $dataUser['birthday'] != "") {
            $dt = explode("-", $dataUser['birthday']);
            $d = $dt[2];
            $m = $dt[1];
            $y = $dt[0];
            $dataUser['birthday'] = $y . "-" . $m . "-" . $d;
        }

        $user_id = $this->session->userdata('logged_in_user')->id;
        $this->Dao->updateUserProfileAll($user_id, $dataUser);
        $this->session->set_userdata('logged_in_user', $this->Dao->findUserById($user_id));
    }

}

?>